{ "cells": [ { "cell_type": "markdown", "source": [ "# The Dice Man\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Dice%20man%20(Solved).ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FDice%20man%20(Solved).ipynb)\n", "\n", "## Problem definition\n", "[The Dice Man](https://en.wikipedia.org/wiki/The_Dice_Man) is a novel where a psychiatrist ends up making all his decisions casting dice. Everytime he faces a decision, he numbers his options from 1 to 6, casts dice, and guides his choices by the result.\n", "Let´s make the dice man bot! a digital assistant for anyone who would like to follow the same decision-making strategy as the Dice Man. The bot must ask the user to define up to 6 options, select one randomly, and show it to the user.\n", "\n" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Solution\n", "In our solution, we are going to use the library [random](https://docs.python.org/3/library/random.html) and its function ```random.randint(a,b)``` which returns a random number between a and b:\n" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am the Dice Bot, what are your options?\n", "1: ´Go out\n", "2: ´Study Python\n", "The dice found an answer, you should choose:\n", "2: ´Study Python\n" ] } ], "source": [ "import random\n", "options_list = [] # We will create an empty list to store the options\n", "n = 0 # This variable keeps track of the number of options introduced by the user\n", "print(\"I am the Dice Bot, what are your options?\")\n", "for i in range(6): # Let´s allow at most 6 options\n", " option = input(\"Tell me an option (press Enter to stop): \")\n", " if option: # We check that the option is not empty (an empty string computes to false)\n", " options_list.append(option)\n", " print(f\"{i+1}: ´{options_list[i]}\")\n", " n+=1\n", " else:\n", " break\n", "\n", "while True: # Let´s cast the dice until we have a valid option\n", " decision = random.randint(1, 6) - 1 #We get a number from 1-6 from dice, then we subtract 1 to get a valid index\n", " if decision < n:\n", " print(\"The dice found an answer, you should choose:\")\n", " print(f\"{decision+1}: ´{options_list[decision]}\")\n", " break\n", "\n" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Analysis questions\n", "These questions are designed to help you better understand the code above. Try to answer them by yourself before asking for help.\n", "\n", "1. **User Input Management:** Why does the program use a loop to get user input for options? How does it determine when to exit the loop? Explain the condition ```if option:```. What purpose does it serve\n", "\n", "2. **Code Adaptability:** The code allows a user to input at most 6 options. Why is this number chosen, and how does it relate to the concept of dice rolls? How easy would it be to modify the program to work with a different number-sided die, for instance, a 12-sided die? What changes would be needed?\n", "\n", "3. **Decision Mechanism:** Describe the process by which the program simulates a dice roll to make a decision. Why is the variable ```decision``` assigned the value ```random.randint(1, 6) - 1``` How does the program ensure that only valid options are chosen?\n", "\n", "4. **While Loop:** The code contains the line ```while True:```. Why is this used? and how does the program ensure it doesn't get stuck in an infinite loop?\n", "\n", "5. **Randomness:** How does the random.randint function ensure that the decision-making process is random and unbiased? Could the program be modified to make it biased towards certain options?\n" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }